package com.yeu;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Index
 */
@WebServlet("/GetTopic")
public class GetTopic extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetTopic() {
		super();
		// TODO Auto-generated constructor stub
	}

	/** 获取真实用户IP **/
	public String getRemortIP(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		} else {
			return request.getHeader("x-forwarded-for");
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式
		PrintWriter pw = response.getWriter();
		String topic = "";
		String topicLimit = request.getParameter("topicLimit");
		String ipAddress = getRemortIP(request);
		String mLog = "";

		/* 获取信息模块 */
		try {
			Connection connect = DBUtil.getConnect();
			Statement statement = (Statement) connect.createStatement(); // Statement可以理解为数据库操作实例，对数据库的所有操作都通过它来实现
			ResultSet result;

			// 填写topic
			// sqlQuery = "select * from " + DBUtil.TABLE_USER_TOPIC + " order
			// by topicDate desc limit 20";
			String sqlQuery = "select * from " + DBUtil.TABLE_USER_TOPIC + " left join " + DBUtil.TABLE_USER_INFO + " on " + DBUtil.TABLE_USER_TOPIC + ".userNumber=" + DBUtil.TABLE_USER_INFO + ".userNumber order by topicDate desc limit " + topicLimit + ",20";
			// 查询类操作返回一个ResultSet集合，没有查到结果时ResultSet的长度为0
			result = statement.executeQuery(sqlQuery); // 先查询同样的账号（比如手机号）是否正确
			while (result.next()) {
				
				topic = topic + "<li class=\"panel panel-default\" style=\"list-style-type:none\">\n" +
		                "                        <div class=\"panel-body\">\n" +
		                "                            <img src=\"userHead/"+ result.getString("userHead") +".jpg\" class=\"img-circle\" width=\"50px\" style=\"float: left\"/>\n" +
		                "                            <div style=\"float: left;margin-left: 10px\">\n" +
		                "                                <p class=\"h4\" style=\"margin-top: 5px\">"+ result.getString("userName") +"</p>\n" +
		                "                                <p class=\"h6\">"+ result.getString("topicDate").substring(0,19) +"</p>\n" +
		                "                            </div>\n" +
		                "                            <div class=\" clearfix\"></div>\n" +
		                "                            <p style=\"margin-top: 15px\">"+ result.getString("topicContent") +"</p>\n" +
		                "                            <p id=\"click\" onclick=\"clickTopic(this)\" class=\"id"+ result.getString("topicId") +"\" style=\"cursor: pointer;float:right;text-decoration:none;margin-right: 15px\"><i class=\"glyphicon glyphicon-thumbs-up\"></i>&nbsp点赞&nbsp<span>"+ result.getString("topicClick") +"</span></p>\n" +
		                "\t\t\t\t\t\t\t<p id=\"showReply\" onclick=\"showReply(this)\" class=\"id"+ result.getString("topicId") +"\" style=\"cursor: pointer;float:right;text-decoration:none;margin-right: 20px\"><i class=\"glyphicon glyphicon-comment\"></i>&nbsp评论&nbsp<span>"+ result.getString("topicTalk") +"</span></p>\n" +
		                "                            <div class=\" clearfix\"></div>\n" +
		                "                            <div class=\"input-group\" style=\"margin-top: 10px\">\n" +
		                "                                <input type=\"text\" class=\"form-control\" id=\"replyContent\">\n" +
		                "                                <span class=\"input-group-btn\"><button class=\"btn btn-default\" id=\"reply\"  onclick=\"replyTopic(this)\">评论</button></span>\n" +
		                "                                <p class=\"id"+ result.getString("topicId") +"\"></p>\n" +
		                "                            </div>\n" +
		                "                            <span></span>\n" +
		                "                        </div>\n" +
		                "                    </li>";
			}
			
			pw.println(topic);
			// 断开数据库连接
			statement.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
